home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / ui4 / systemtray.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  21KB  |  579 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import struct
  6. import select
  7. import os
  8. import signal
  9. import os.path as os
  10. import time
  11. from base.g import *
  12. from base import device, utils, models
  13. from base.codes import *
  14. from ui_utils import *
  15.  
  16. try:
  17.     from PyQt4.QtCore import *
  18.     from PyQt4.QtGui import *
  19. except ImportError:
  20.     log.error('Python bindings for Qt4 not found. Try using --qt3. Exiting!')
  21.     sys.exit(1)
  22.  
  23. from systrayframe import SystrayFrame
  24.  
  25. try:
  26.     import dbus
  27.     from dbus import SessionBus, lowlevel
  28. except ImportError:
  29.     log.error('Python bindings for dbus not found. Exiting!')
  30.     sys.exit(1)
  31.  
  32. import warnings
  33. warnings.simplefilter('ignore', DeprecationWarning)
  34. have_pynotify = True
  35.  
  36. try:
  37.     import pynotify
  38. except ImportError:
  39.     have_pynotify = False
  40.  
  41. TRAY_MESSAGE_DELAY = 10000
  42. HIDE_INACTIVE_DELAY = 5000
  43. BLIP_DELAY = 2000
  44. SET_MENU_DELAY = 1000
  45. MAX_MENU_EVENTS = 10
  46. ERROR_STATE_TO_ICON = {
  47.     ERROR_STATE_CLEAR: QSystemTrayIcon.Information,
  48.     ERROR_STATE_OK: QSystemTrayIcon.Information,
  49.     ERROR_STATE_WARNING: QSystemTrayIcon.Warning,
  50.     ERROR_STATE_ERROR: QSystemTrayIcon.Critical,
  51.     ERROR_STATE_LOW_SUPPLIES: QSystemTrayIcon.Warning,
  52.     ERROR_STATE_BUSY: QSystemTrayIcon.Warning,
  53.     ERROR_STATE_LOW_PAPER: QSystemTrayIcon.Warning,
  54.     ERROR_STATE_PRINTING: QSystemTrayIcon.Information,
  55.     ERROR_STATE_SCANNING: QSystemTrayIcon.Information,
  56.     ERROR_STATE_PHOTOCARD: QSystemTrayIcon.Information,
  57.     ERROR_STATE_FAXING: QSystemTrayIcon.Information,
  58.     ERROR_STATE_COPYING: QSystemTrayIcon.Information }
  59. if have_pynotify:
  60.     info = getPynotifyIcon('info')
  61.     warn = getPynotifyIcon('warning')
  62.     err = getPynotifyIcon('error')
  63.     ERROR_STATE_TO_ICON_AND_URGENCY_PYNOTIFY = {
  64.         ERROR_STATE_CLEAR: (info, pynotify.URGENCY_LOW),
  65.         ERROR_STATE_OK: (info, pynotify.URGENCY_LOW),
  66.         ERROR_STATE_WARNING: (warn, pynotify.URGENCY_NORMAL),
  67.         ERROR_STATE_ERROR: (err, pynotify.URGENCY_CRITICAL),
  68.         ERROR_STATE_LOW_SUPPLIES: (warn, pynotify.URGENCY_NORMAL),
  69.         ERROR_STATE_BUSY: (warn, pynotify.URGENCY_NORMAL),
  70.         ERROR_STATE_LOW_PAPER: (warn, pynotify.URGENCY_NORMAL),
  71.         ERROR_STATE_PRINTING: (info, pynotify.URGENCY_LOW),
  72.         ERROR_STATE_SCANNING: (info, pynotify.URGENCY_LOW),
  73.         ERROR_STATE_PHOTOCARD: (info, pynotify.URGENCY_LOW),
  74.         ERROR_STATE_FAXING: (info, pynotify.URGENCY_LOW),
  75.         ERROR_STATE_COPYING: (info, pynotify.URGENCY_LOW) }
  76.  
  77. devices = { }
  78.  
  79. class DeviceMenu(QMenu):
  80.     
  81.     def __init__(self, title, parent, device_uri, device_hist, index):
  82.         QMenu.__init__(self, title, parent)
  83.         self.device_uri = device_uri
  84.         self.device_hist = device_hist
  85.         self.index = index
  86.  
  87.     
  88.     def update(self):
  89.         self.clear()
  90.         if self.device_hist:
  91.             first = True
  92.             for e in self.device_hist:
  93.                 error_state = STATUS_TO_ERROR_STATE_MAP.get(e.event_code, ERROR_STATE_CLEAR)
  94.                 ess = device.queryString(e.event_code, 0)
  95.                 a = QAction(QIcon(getStatusListIcon(error_state)[self.index]), QString('%1 %2').arg(ess).arg(getTimeDeltaDesc(e.timedate)), self)
  96.                 if first:
  97.                     f = a.font()
  98.                     f.setBold(True)
  99.                     a.setFont(f)
  100.                     self.setIcon(QIcon(getStatusListIcon(error_state)[self.index]))
  101.                     first = False
  102.                 
  103.                 self.addAction(a)
  104.             
  105.         else:
  106.             self.addAction(QIcon(load_pixmap('warning', '16x16')), QApplication.translate('SystemTray', '(No events)', None, QApplication.UnicodeUTF8))
  107.  
  108.  
  109.  
  110. class HistoryDevice(QObject):
  111.     
  112.     def __init__(self, device_uri, needs_update = True):
  113.         self.needs_update = needs_update
  114.         self.device_uri = device_uri
  115.         (back_end, is_hp, bus, model, serial, dev_file, host, zc, port) = device.parseDeviceURI(device_uri)
  116.         if bus == 'usb':
  117.             self.id = serial
  118.         elif bus == 'net':
  119.             self.id = host
  120.         elif bus == 'par':
  121.             self.id = dev_file
  122.         else:
  123.             self.id = 'unknown'
  124.         self.model = models.normalizeModelUIName(model)
  125.         if back_end == 'hp':
  126.             self.device_type = DEVICE_TYPE_PRINTER
  127.             self.menu_text = self._HistoryDevice__tr('%1 Printer (%2)').arg(self.model).arg(self.id)
  128.         elif back_end == 'hpaio':
  129.             self.device_type = DEVICE_TYPE_SCANNER
  130.             self.menu_text = self._HistoryDevice__tr('%1 Scanner (%2)').arg(self.model).arg(self.id)
  131.         elif back_end == 'hpfax':
  132.             self.device_type = DEVICE_TYPE_FAX
  133.             self.menu_text = self._HistoryDevice__tr('%1 Fax (%2)').arg(self.model).arg(self.id)
  134.         else:
  135.             self.device_type = DEVICE_TYPE_UNKNOWN
  136.             self.menu_text = self._HistoryDevice__tr('%1 (%2)').arg(self.model).arg(self.id)
  137.         self.mq = device.queryModelByURI(self.device_uri)
  138.         self.index = 0
  139.         if self.mq.get('tech-type', TECH_TYPE_NONE) in (TECH_TYPE_MONO_LASER, TECH_TYPE_COLOR_LASER):
  140.             self.index = 1
  141.         
  142.         self.history = None
  143.  
  144.     
  145.     def getHistory(self, service):
  146.         pass
  147.  
  148.     
  149.     def __tr(self, s, c = None):
  150.         return QApplication.translate('SystemTray', s, c, QApplication.UnicodeUTF8)
  151.  
  152.  
  153.  
  154. class SystraySettingsDialog(QDialog):
  155.     
  156.     def __init__(self, parent, systray_visible, polling, polling_interval, systray_messages, device_list = None):
  157.         QDialog.__init__(self, parent)
  158.         self.systray_visible = systray_visible
  159.         self.systray_messages = systray_messages
  160.         if device_list is not None:
  161.             self.device_list = device_list
  162.         else:
  163.             self.device_list = { }
  164.         self.polling = polling
  165.         self.polling_interval = polling_interval
  166.         self.initUi()
  167.         self.SystemTraySettings.updateUi()
  168.  
  169.     
  170.     def initUi(self):
  171.         self.setObjectName('SystraySettingsDialog')
  172.         self.resize(QSize(QRect(0, 0, 488, 565).size()).expandedTo(self.minimumSizeHint()))
  173.         self.gridlayout = QGridLayout(self)
  174.         self.gridlayout.setObjectName('gridlayout')
  175.         self.SystemTraySettings = SystrayFrame(self)
  176.         self.SystemTraySettings.initUi(self.systray_visible, self.polling, self.polling_interval, self.device_list, self.systray_messages)
  177.         sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
  178.         sizePolicy.setHorizontalStretch(0)
  179.         sizePolicy.setVerticalStretch(0)
  180.         sizePolicy.setHeightForWidth(self.SystemTraySettings.sizePolicy().hasHeightForWidth())
  181.         self.SystemTraySettings.setSizePolicy(sizePolicy)
  182.         self.SystemTraySettings.setFrameShadow(QFrame.Raised)
  183.         self.SystemTraySettings.setObjectName('SystemTraySettings')
  184.         self.gridlayout.addWidget(self.SystemTraySettings, 0, 0, 1, 2)
  185.         spacerItem = QSpacerItem(301, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
  186.         self.gridlayout.addItem(spacerItem, 1, 0, 1, 1)
  187.         self.StdButtons = QDialogButtonBox(self)
  188.         self.StdButtons.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.NoButton | QDialogButtonBox.Ok)
  189.         self.StdButtons.setCenterButtons(False)
  190.         self.StdButtons.setObjectName('StdButtons')
  191.         self.gridlayout.addWidget(self.StdButtons, 1, 1, 1, 1)
  192.         QObject.connect(self.StdButtons, SIGNAL('accepted()'), self.acceptClicked)
  193.         QObject.connect(self.StdButtons, SIGNAL('rejected()'), self.reject)
  194.         self.setWindowTitle(self._SystraySettingsDialog__tr('HP Device Manager - System Tray Settings'))
  195.  
  196.     
  197.     def acceptClicked(self):
  198.         self.systray_visible = self.SystemTraySettings.systray_visible
  199.         self.polling = self.SystemTraySettings.polling
  200.         self.polling_interval = self.SystemTraySettings.polling_interval
  201.         self.device_list = self.SystemTraySettings.device_list
  202.         self.systray_messages = self.SystemTraySettings.systray_messages
  203.         self.accept()
  204.  
  205.     
  206.     def __tr(self, s, c = None):
  207.         return QApplication.translate('SystraySettingsDialog', s, c, QApplication.UnicodeUTF8)
  208.  
  209.  
  210.  
  211. class SystemTrayApp(QApplication):
  212.     
  213.     def __init__(self, args, read_pipe):
  214.         QApplication.__init__(self, args)
  215.         self.menu = None
  216.         self.read_pipe = read_pipe
  217.         self.fmt = '64s64sI32sI64sf'
  218.         self.fmt_size = struct.calcsize(self.fmt)
  219.         self.timer_active = False
  220.         self.active_icon = False
  221.         self.user_settings = UserSettings()
  222.         self.user_settings.load()
  223.         self.user_settings.debug()
  224.         self.tray_icon = QSystemTrayIcon()
  225.         pm = load_pixmap('hp_logo', '32x32')
  226.         self.prop_icon = QIcon(pm)
  227.         a = load_pixmap('active', '16x16')
  228.         painter = QPainter(pm)
  229.         painter.drawPixmap(32, 0, a)
  230.         painter.end()
  231.         self.prop_active_icon = QIcon(pm)
  232.         self.tray_icon.setIcon(self.prop_icon)
  233.         self.session_bus = SessionBus()
  234.         self.service = None
  235.         for d in device.getSupportedCUPSDevices(back_end_filter = [
  236.             'hp',
  237.             'hpfax']):
  238.             self.addDevice(d)
  239.         
  240.         self.tray_icon.setToolTip(self._SystemTrayApp__tr('HPLIP Status Service'))
  241.         QObject.connect(self.tray_icon, SIGNAL('messageClicked()'), self.messageClicked)
  242.         notifier = QSocketNotifier(self.read_pipe, QSocketNotifier.Read)
  243.         QObject.connect(notifier, SIGNAL('activated(int)'), self.notifierActivated)
  244.         QObject.connect(self.tray_icon, SIGNAL('activated(QSystemTrayIcon::ActivationReason)'), self.trayActivated)
  245.         self.tray_icon.show()
  246.         if self.user_settings.systray_visible == SYSTRAY_VISIBLE_SHOW_ALWAYS:
  247.             self.tray_icon.setVisible(True)
  248.         else:
  249.             QTimer.singleShot(HIDE_INACTIVE_DELAY, self.timeoutHideWhenInactive)
  250.         self.tray_icon.setIcon(self.prop_active_icon)
  251.         self.active_icon = True
  252.         QTimer.singleShot(SET_MENU_DELAY, self.initDone)
  253.  
  254.     
  255.     def initDone(self):
  256.         self.tray_icon.setIcon(self.prop_icon)
  257.         self.active_icon = False
  258.         self.setMenu()
  259.  
  260.     
  261.     def addDevice(self, device_uri):
  262.         
  263.         try:
  264.             devices[device_uri]
  265.         except KeyError:
  266.             devices[device_uri] = HistoryDevice(device_uri)
  267.  
  268.         devices[device_uri].needs_update = True
  269.  
  270.     
  271.     def setMenu(self):
  272.         self.menu = QMenu()
  273.         title = QWidgetAction(self.menu)
  274.         hbox = QFrame(self.menu)
  275.         layout = QHBoxLayout(hbox)
  276.         layout.setMargin(3)
  277.         layout.setSpacing(5)
  278.         pix_label = QLabel(hbox)
  279.         layout.insertWidget(-1, pix_label, 0)
  280.         icon_size = self.menu.style().pixelMetric(QStyle.PM_SmallIconSize)
  281.         pix_label.setPixmap(self.prop_icon.pixmap(icon_size))
  282.         label = QLabel(hbox)
  283.         layout.insertWidget(-1, label, 20)
  284.         title.setDefaultWidget(hbox)
  285.         label.setText(self._SystemTrayApp__tr('HPLIP Status Service'))
  286.         f = label.font()
  287.         f.setBold(True)
  288.         label.setFont(f)
  289.         self.menu.insertAction(None, title)
  290.         if devices:
  291.             if self.service is None:
  292.                 t = 0
  293.                 while t < 3:
  294.                     
  295.                     try:
  296.                         self.service = self.session_bus.get_object('com.hplip.StatusService', '/com/hplip/StatusService')
  297.                     except DBusException:
  298.                         log.warn('Unable to connect to StatusService. Retrying...')
  299.  
  300.                     t += 1
  301.                     time.sleep(0.5)
  302.             
  303.             if self.service is not None:
  304.                 self.menu.addSeparator()
  305.                 for d in devices:
  306.                     devices[d].getHistory(self.service)
  307.                     menu = DeviceMenu(devices[d].menu_text, self.menu, d, devices[d].history, devices[d].index)
  308.                     self.menu.addMenu(menu)
  309.                     menu.update()
  310.                 
  311.             
  312.         
  313.         self.menu.addSeparator()
  314.         self.menu.addAction(self._SystemTrayApp__tr('HP Device Manager...'), self.toolboxTriggered)
  315.         self.menu.addSeparator()
  316.         self.settings_action = self.menu.addAction(QIcon(load_pixmap('settings', '16x16')), self._SystemTrayApp__tr('Settings...'), self.settingsTriggered)
  317.         self.menu.addSeparator()
  318.         self.menu.addAction(QIcon(load_pixmap('quit', '16x16')), 'Quit', self.quitTriggered)
  319.         self.tray_icon.setContextMenu(self.menu)
  320.  
  321.     
  322.     def settingsTriggered(self):
  323.         if self.menu is None:
  324.             return None
  325.         self.sendMessage('', '', EVENT_DEVICE_STOP_POLLING)
  326.         
  327.         try:
  328.             dlg = SystraySettingsDialog(self.menu, self.user_settings.systray_visible, self.user_settings.polling, self.user_settings.polling_interval, self.user_settings.systray_messages, self.user_settings.polling_device_list)
  329.             if dlg.exec_() == QDialog.Accepted:
  330.                 self.user_settings.systray_visible = dlg.systray_visible
  331.                 self.user_settings.systray_messages = dlg.systray_messages
  332.                 self.user_settings.save()
  333.                 if self.user_settings.systray_visible == SYSTRAY_VISIBLE_SHOW_ALWAYS:
  334.                     log.debug('Showing...')
  335.                     self.tray_icon.setVisible(True)
  336.                 else:
  337.                     log.debug('Waiting to hide...')
  338.                     QTimer.singleShot(HIDE_INACTIVE_DELAY, self.timeoutHideWhenInactive)
  339.                 self.sendMessage('', '', EVENT_USER_CONFIGURATION_CHANGED)
  340.         finally:
  341.             self.sendMessage('', '', EVENT_DEVICE_START_POLLING)
  342.  
  343.  
  344.     
  345.     def timeoutHideWhenInactive(self):
  346.         log.debug('Hiding...')
  347.         if self.user_settings.systray_visible in (SYSTRAY_VISIBLE_HIDE_WHEN_INACTIVE, SYSTRAY_VISIBLE_HIDE_ALWAYS):
  348.             self.tray_icon.setVisible(False)
  349.             log.debug('Hidden')
  350.         
  351.  
  352.     
  353.     def updateMenu(self):
  354.         if self.menu is None:
  355.             return None
  356.         for a in self.menu.actions():
  357.             
  358.             try:
  359.                 a.menu().update()
  360.             continue
  361.             except AttributeError:
  362.                 self.menu is None
  363.                 self.menu is None
  364.                 continue
  365.                 continue
  366.             
  367.  
  368.         
  369.  
  370.     
  371.     def trayActivated(self, reason):
  372.         if reason == QSystemTrayIcon.Context:
  373.             self.updateMenu()
  374.         elif reason == QSystemTrayIcon.DoubleClick:
  375.             self.toolboxTriggered()
  376.         elif reason == QSystemTrayIcon.Trigger:
  377.             pass
  378.         elif reason == QSystemTrayIcon.MiddleClick:
  379.             pass
  380.         
  381.  
  382.     
  383.     def messageClicked(self):
  384.         pass
  385.  
  386.     
  387.     def quitTriggered(self):
  388.         log.debug('Exiting')
  389.         self.sendMessage('', '', EVENT_SYSTEMTRAY_EXIT)
  390.         self.quit()
  391.  
  392.     
  393.     def toolboxTriggered(self):
  394.         
  395.         try:
  396.             os.waitpid(-1, os.WNOHANG)
  397.         except OSError:
  398.             pass
  399.  
  400.         (ok, lock_file) = utils.lock_app('hp-toolbox', True)
  401.         if ok:
  402.             utils.unlock(lock_file)
  403.             path = utils.which('hp-toolbox')
  404.             if path:
  405.                 path = os.path.join(path, 'hp-toolbox')
  406.             else:
  407.                 self.tray_icon.showMessage(self._SystemTrayApp__tr('HPLIP Status Service'), self._SystemTrayApp__tr('Unable to locate hp-toolbox on system PATH.'), QSystemTrayIcon.Critical, TRAY_MESSAGE_DELAY)
  408.                 log.error('Unable to find hp-toolbox on PATH.')
  409.                 return None
  410.             path.debug('Running hp-toolbox: hp-toolbox')
  411.             os.spawnlp(os.P_NOWAIT, path, 'hp-toolbox')
  412.         else:
  413.             self.sendMessage('', '', EVENT_RAISE_DEVICE_MANAGER, interface = 'com.hplip.Toolbox')
  414.  
  415.     
  416.     def sendMessage(self, device_uri, printer_name, event_code, username = prop.username, job_id = 0, title = '', pipe_name = '', interface = 'com.hplip.StatusService'):
  417.         device.Event(device_uri, printer_name, event_code, username, job_id, title).send_via_dbus(self.session_bus, interface)
  418.  
  419.     
  420.     def notifierActivated(self, s):
  421.         m = ''
  422.         while True:
  423.             
  424.             try:
  425.                 (r, w, e) = select.select([
  426.                     self.read_pipe], [], [
  427.                     self.read_pipe], 1)
  428.             except select.error:
  429.                 log.debug('Error in select()')
  430.                 break
  431.  
  432.             if e:
  433.                 log.error('Pipe error: %s' % e)
  434.                 break
  435.             
  436.             if r:
  437.                 m = ''.join([
  438.                     m,
  439.                     os.read(self.read_pipe, self.fmt_size)])
  440.                 while len(m) >= self.fmt_size:
  441.                     event = device.Event(*struct.unpack(self.fmt, m[:self.fmt_size]))
  442.                     m = m[self.fmt_size:]
  443.                     if event.event_code == EVENT_USER_CONFIGURATION_CHANGED:
  444.                         log.debug('Re-reading configuration (EVENT_USER_CONFIGURATION_CHANGED)')
  445.                         self.user_settings.load()
  446.                         self.user_settings.debug()
  447.                     elif event.event_code == EVENT_SYSTEMTRAY_EXIT:
  448.                         self.quit()
  449.                         return None
  450.                     if self.user_settings.systray_visible in (SYSTRAY_VISIBLE_SHOW_ALWAYS, SYSTRAY_VISIBLE_HIDE_WHEN_INACTIVE):
  451.                         log.debug('Showing...')
  452.                         self.tray_icon.setVisible(True)
  453.                         if event.event_code == EVENT_DEVICE_UPDATE_ACTIVE:
  454.                             if not self.active_icon:
  455.                                 self.tray_icon.setIcon(self.prop_active_icon)
  456.                                 self.active_icon = True
  457.                                 continue
  458.                             continue
  459.                         elif event.event_code == EVENT_DEVICE_UPDATE_INACTIVE:
  460.                             if self.active_icon:
  461.                                 self.tray_icon.setIcon(self.prop_icon)
  462.                                 self.active_icon = False
  463.                                 continue
  464.                             continue
  465.                         elif event.event_code == EVENT_DEVICE_UPDATE_BLIP:
  466.                             if not self.active_icon:
  467.                                 self.tray_icon.setIcon(self.prop_active_icon)
  468.                                 self.active_icon = True
  469.                                 QTimer.singleShot(BLIP_DELAY, self.blipTimeout)
  470.                                 continue
  471.                             continue
  472.                         
  473.                     
  474.                     if self.user_settings.systray_visible in (SYSTRAY_VISIBLE_HIDE_WHEN_INACTIVE, SYSTRAY_VISIBLE_HIDE_ALWAYS):
  475.                         log.debug('Waiting to hide...')
  476.                         QTimer.singleShot(HIDE_INACTIVE_DELAY, self.timeoutHideWhenInactive)
  477.                     
  478.                     if event.event_code <= EVENT_MAX_USER_EVENT:
  479.                         self.addDevice(event.device_uri)
  480.                         self.setMenu()
  481.                         if self.tray_icon.supportsMessages():
  482.                             log.debug('Tray icon message:')
  483.                             event.debug()
  484.                             error_state = STATUS_TO_ERROR_STATE_MAP.get(event.event_code, ERROR_STATE_CLEAR)
  485.                             desc = device.queryString(event.event_code)
  486.                             show_message = False
  487.                             if self.user_settings.systray_messages == SYSTRAY_MESSAGES_SHOW_ALL:
  488.                                 show_message = True
  489.                             elif self.user_settings.systray_messages in (SYSTRAY_MESSAGES_SHOW_ERRORS_AND_WARNINGS, SYSTRAY_MESSAGES_SHOW_ERRORS_ONLY):
  490.                                 if error_state == ERROR_STATE_ERROR:
  491.                                     show_message = True
  492.                                 elif self.user_settings.systray_messages == SYSTRAY_MESSAGES_SHOW_ERRORS_AND_WARNINGS and error_state in (ERROR_STATE_WARNING, ERROR_STATE_LOW_SUPPLIES, ERROR_STATE_LOW_PAPER):
  493.                                     show_message = True
  494.                                 
  495.                             
  496.                             if event.printer_name:
  497.                                 d = QString(event.printer_name)
  498.                             else:
  499.                                 (back_end, is_hp, bus, model, serial, dev_file, host, zc, port) = device.parseDeviceURI(event.device_uri)
  500.                                 if bus == 'usb':
  501.                                     idd = serial
  502.                                 elif bus == 'net':
  503.                                     idd = host
  504.                                 elif bus == 'par':
  505.                                     idd = dev_file
  506.                                 else:
  507.                                     idd = 'unknown'
  508.                                 self.model = models.normalizeModelUIName(model)
  509.                                 if back_end == 'hp':
  510.                                     d = self._SystemTrayApp__tr('%1 Printer (%2)').arg(model).arg(idd)
  511.                                 elif back_end == 'hpaio':
  512.                                     d = self._SystemTrayApp__tr('%1 Scanner (%2)').arg(model).arg(idd)
  513.                                 elif back_end == 'hpfax':
  514.                                     d = self._SystemTrayApp__tr('%1 Fax (%2)').arg(model).arg(idd)
  515.                                 else:
  516.                                     d = self._SystemTrayApp__tr('%1 (%2)').arg(model).arg(idd)
  517.                             if show_message:
  518.                                 if have_pynotify and pynotify.init('hplip'):
  519.                                     (icon, urgency) = ERROR_STATE_TO_ICON_AND_URGENCY_PYNOTIFY.get(error_state, (getPynotifyIcon('info'), pynotify.URGENCY_NORMAL))
  520.                                     if event.job_id and event.title:
  521.                                         msg = '%s\n%s: %s\n(%s/%s)' % (unicode(d), desc, event.title, event.username, event.job_id)
  522.                                         log.debug('Notify: uri=%s desc=%s title=%s user=%s job_id=%d code=%d' % (event.device_uri, desc, event.title, event.username, event.job_id, event.event_code))
  523.                                     else:
  524.                                         msg = '%s\n%s (%s)' % (unicode(d), desc, event.event_code)
  525.                                         log.debug('Notify: uri=%s desc=%s code=%d' % (event.device_uri, desc, event.event_code))
  526.                                     n = pynotify.Notification('HPLIP Device Status', msg, icon)
  527.                                     n.set_urgency(urgency)
  528.                                     if error_state == ERROR_STATE_ERROR:
  529.                                         n.set_timeout(pynotify.EXPIRES_NEVER)
  530.                                     else:
  531.                                         n.set_timeout(TRAY_MESSAGE_DELAY)
  532.                                     n.show()
  533.                                 else:
  534.                                     icon = ERROR_STATE_TO_ICON.get(error_state, QSystemTrayIcon.Information)
  535.                                     if event.job_id and event.title:
  536.                                         log.debug('Bubble: uri=%s desc=%s title=%s user=%s job_id=%d code=%d' % (event.device_uri, desc, event.title, event.username, event.job_id, event.event_code))
  537.                                         self.tray_icon.showMessage(self._SystemTrayApp__tr('HPLIP Device Status'), QString('%1\n%2: %3\n(%4/%5)').arg(d).arg(desc).arg(event.title).arg(event.username).arg(event.job_id), icon, TRAY_MESSAGE_DELAY)
  538.                                     else:
  539.                                         log.debug('Bubble: uri=%s desc=%s code=%d' % (event.device_uri, desc, event.event_code))
  540.                                         self.tray_icon.showMessage(self._SystemTrayApp__tr('HPLIP Device Status'), QString('%1\n%2 (%3)').arg(d).arg(desc).arg(event.event_code), icon, TRAY_MESSAGE_DELAY)
  541.                             
  542.                         
  543.                     self.tray_icon.supportsMessages()
  544.                 continue
  545.             break
  546.  
  547.     
  548.     def blipTimeout(self):
  549.         if self.active_icon:
  550.             self.tray_icon.setIcon(self.prop_icon)
  551.             self.active_icon = False
  552.         
  553.  
  554.     
  555.     def __tr(self, s, c = None):
  556.         return QApplication.translate('SystemTray', s, c, QApplication.UnicodeUTF8)
  557.  
  558.  
  559.  
  560. def run(read_pipe):
  561.     log.set_module('hp-systray(qt4)')
  562.     log.debug('PID=%d' % os.getpid())
  563.     app = SystemTrayApp(sys.argv, read_pipe)
  564.     app.setQuitOnLastWindowClosed(False)
  565.     i = 0
  566.     while i < 10:
  567.         if QSystemTrayIcon.isSystemTrayAvailable():
  568.             break
  569.         
  570.         time.sleep(1)
  571.         i += 1
  572.     if not QSystemTrayIcon.isSystemTrayAvailable():
  573.         FailureUI(None, QApplication.translate('SystemTray', '<b>No system tray detected on this system.</b><p>Unable to start, exiting.</p>', None, QApplication.UnicodeUTF8), QApplication.translate('SystemTray', 'HPLIP Status Service', None, QApplication.UnicodeUTF8))
  574.     else:
  575.         notifier = QSocketNotifier(read_pipe, QSocketNotifier.Read)
  576.         QObject.connect(notifier, SIGNAL('activated(int)'), app.notifierActivated)
  577.         app.exec_()
  578.  
  579.